<template>
	<ele-modal
	  :width="950"
	  :visible="visible"
	  :confirm-loading="loading"
	  :title="'手工录入'"
	  :body-style="{ paddingBottom: '30px' }"
	  :maskClosable="false"
	  @update:visible="updateVisible"
	  @ok="save"
	>
		<div>
			<a-form :label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
				:wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }">
				<a-row :gutter="24">
					<a-col :md="24" :sm="24" :xs="24">
						<a-row>
							<a-col :md="8" :sm="8" :xs="8">
								<a-form-item label="本司货品" :colon="false">
									<a-radio-group v-model:value="form.original_type">
										<a-radio value="1">是</a-radio>
										<a-radio value="2">否</a-radio>
									</a-radio-group>
								</a-form-item>
							</a-col>
							<a-col :md="8" :sm="8" :xs="8">
								<a-form-item label="换购方式" :colon="false">
									<a-radio-group v-model:value="form.retail_type">
										<a-radio value="1">按件</a-radio>
										<a-radio value="2">按克</a-radio>
									</a-radio-group>
								</a-form-item>
							</a-col>
						</a-row>
					</a-col>
					<a-col :md="24" :sm="24" :xs="24">
						<lu1 v-if="form.original_type == 1"></lu1>
						<lu2 v-else-if="form.original_type == 2"></lu2>
					</a-col>
				</a-row>
			</a-form>
		</div>
	</ele-modal>
</template>

<script setup>
	import {ref,reactive,onMounted, watch} from 'vue';
	import {Modal,message,Form} from 'ant-design-vue';
	import dayjs from 'dayjs';
	import {uploadUrl} from '@/config/setting';
	import {emailReg,phoneReg,identityReg} from 'ele-admin-pro';
	import {PlusOutlined,LoadingOutlined} from '@ant-design/icons-vue';
	import lu1 from './lu1.vue';
	import lu2 from './lu2.vue';
	import {get_buy_detail, junk_exchange} from '@/api/order/order/jewelry_order';
	import {get_list} from '@/api/setting/sales_subject';
	import {get_gold_category_select2, get_subclass_select, get_quality_select, get_gem_category_select, get_classes_select, get_gem_attribute_select} from '@/api/material/select';

	// 表格列配置
	const useForm = Form.useForm;

	const props = defineProps({
		// 弹窗是否打开
		visible: Boolean,
		member_id: [Number, String],
		bar_code:String,
	});

	const emit = defineEmits(['done', 'update:visible', 'get_been_order']);

	const loading = ref(false);
	const signOptions = ref([]);
	const goldOpt = ref([]);
	const subclassOpt = ref([]);
	const qualityOpt = ref([]);
	const gemcategoryOpt = ref([]);
	const classesOpt = ref([]);
	const gemattributeOpt1 = ref([]);
	const gemattributeOpt2 = ref([]);
	const gemattributeOpt3 = ref([]);

	const form = reactive({
		member_id: props.member_id,
		original_type:'1',
		retail_type:'1',
		thumb:'',
		bar_code:'',
		gold_category_code:'',
		gold_category_code_name:'',
		subclass_code:'',
		subclass_code_name:'',
		quality_code:'',
		quality_code_name:'',
		gem_category_code:'',
		gem_category_name:'',
		classes_code:'',
		classes_name:'',
		name:'',
		num:'',
		gold_weight_old:'',
		gold_weight:'',
		piece_weight:'',
		real_quality:'',
		lumping_weight:'',
		sticker_price:'',
		real_amount:'',
		main_stone_weight:'',
		main_stone_color:'',
		main_color_name:'',
		main_stone_neatness:'',
		main_neatness_name:'',
		main_stone_cut:'',
		main_cut_name:'',
		main_stone_num:'',
		vice_stone_weight:'',
		vice_stone_num:'',
		exchange_gold_price:'',
		exchange_labour_type:'1',
		exchange_labour:'',
		deduct_amount:'',
		junk_tag_code:'',
		note:'',
	});
	
	watch(
		() => [
			form.lumping_weight,
			form.exchange_gold_price,
			form.exchange_labour_type,
			form.exchange_labour
		],
		(newValue, oldValue) => {
			console.log('newValue===', newValue);
			//抵扣金额：折足重✖换购金价-换购工费（工费按克：换购工费×折足重，工费按件：直接减工费）
			if(form.exchange_labour_type == 1){
				form.deduct_amount = Number(form.lumping_weight) * Number(form.exchange_gold_price) - Number(form.exchange_labour) *  Number(form.lumping_weight);
			}else if(form.exchange_labour_type == 2){
				form.deduct_amount = Number(form.lumping_weight) * Number(form.exchange_gold_price) - Number(form.exchange_labour);
			}
		}
	);
	
	onMounted(() => {
		if(props.bar_code){
			form.bar_code = props.bar_code;
			search();
		}
		get_list({page: 1,page_size: 9999,filters: {type: 'sales'}}).then((res) => {
			res.list.forEach((item) => {
				signOptions.value.push({
					label: item.name,
					value: item.code
				});
			});
		});
		get_gold_category_select2({page: 1,page_size: 9999}).then((res) => {
			goldOpt.value = res;
		});
		get_subclass_select({page: 1,page_size: 9999}).then((res) => {
			subclassOpt.value = res;
		});
		get_quality_select({page: 1,page_size: 9999}).then((res) => {
			qualityOpt.value = res;
		});
		get_gem_category_select({page: 1,page_size: 9999}).then((res) => {
			gemcategoryOpt.value = res;
		});
		get_classes_select({page: 1,page_size: 9999}).then((res) => {
			classesOpt.value = res;
		});
		get_gem_attribute_select({page: 1,page_size: 9999,filters: {type: 'color'}}).then((res) => {
			gemattributeOpt1.value = res;
		});
		get_gem_attribute_select({page: 1,page_size: 9999,filters: {type: 'neatness'}}).then((res) => {
			gemattributeOpt2.value = res;
		});
		get_gem_attribute_select({page: 1,page_size: 9999,filters: {type: 'cut'}}).then((res) => {
			gemattributeOpt3.value = res;
		});
	});
	
	const changeW = (e)=>{
		console.log(e);
		form.lumping_weight = form.gold_weight * (form.real_quality/100)
	}
	const search = ()=>{
		get_buy_detail({
			member_id: props.member_id,
			bar_code: form.bar_code
		}).then(res=>{
			console.log(res);
			Object.keys(form).forEach(key=>{
				form[key] = res[key] ? res[key] : null;
			})
			form.gold_weight_old = res.gold_weight;
			form.exchange_labour_type = '1';
		})
	}
	const save = ()=>{
		console.log(form);
		junk_exchange(form).then(res=>{
			message.success(res.msg);
			emit('get_been_order');
			updateVisible(false);
		}).catch(e=>{
			message.error(e);
		})
	}

	
	/* 更新visible */
	const updateVisible = (value) => {
	  emit('update:visible', value);
	};
</script>
<style scoped></style>